<template>
  <div class="app">
    <van-divider :style="{ color: '#1989fa', borderColor: '#1989fa', padding: '0 16px' }">
      欢迎登录
    </van-divider>
    <div>
      <van-form @submit="onSubmit">
        <van-field
          v-model="phone"
          name="手机号"
          label="手机号"
          placeholder="请输入手机号"
          :rules="[
            { required: true, message: '请填写手机号' },
            { pattern: /^1[3456789]\d{9}$/, message: '手机号格式有误' },
          ]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit">登录</van-button>
          <van-button round block type="info" @click.stop="register()" class="register"
            >去注册</van-button
          >
          <van-button round block type="info" @click.stop="code()" class="register"
            >验证码登录</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { Indicator } from "mint-ui";
import { Toast } from "mint-ui";
import url from "../tools/url";
export default {
  name: "Login",
  data() {
    return {
      phone: "",
      password: "",
    };
  },
  methods: {
    register() {
      this.$router.push("/Register");
    },
    code() {
      this.$router.push("/byCode");
    },
    onSubmit() {
      Indicator.open("加载中...");
      var str = `phone=${this.phone}&password=${this.password}`;
      this.axios.post(url.LOGIN_BY_PASSWORD, str).then((res) => {
        if (res.data.code == 200) {
          Indicator.close();
          Toast(res.data.message);
          this.$store.commit("saveInfo", res.data.data);
          sessionStorage.setItem("token", res.data.data.token);
          this.$router.push("/HelloWorld");
        } else {
          Toast(res.data.message);
        }
      });
    },
  },
};
</script>

<style scope>
.app {
  margin-top: 45px;
}
.btn {
  margin-top: 5px;
}
.register {
  margin-top: 10px;
}
</style>
